package com.egao.common.module.cms.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.egao.common.core.exception.ParameterException;
import com.egao.common.core.web.JsonResult;
import com.egao.common.core.web.PageParam;
import com.egao.common.module.cms.entity.Category;
import com.egao.common.module.cms.mapper.CategoryMapper;
import com.egao.common.module.cms.service.CategoryService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 栏目管理实现类
 *
 * @author pyx
 */
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {

    @Override
    public JsonResult treeAllList(PageParam<Category> pageParam) {
        List<Category> categoryList = baseMapper.selectTree(pageParam);

        // 构建树形对象
        ArrayList<Category> tree = new ArrayList<>();

        // 一级树
        for (Category one : categoryList) {
            if (one.getParentId() == 0) {
                tree.add(one);
            }
        }

        // 二级树
        for (Category t : tree) {
            ArrayList<Category> childList = new ArrayList<>();
            for (Category c : categoryList) {
                if (c.getParentId() != 0 && c.getParentId().equals(t.getId())) {
                    childList.add(c);
                }
            }

            t.setChildren(childList);

            if (t.getChildren().size() == 0) {
                t.setChildren(null);
            }
            //把查询到的子类数据移除
            categoryList.removeAll(childList);
        }

        //把查询到的父类数据移除
        categoryList.removeAll(tree);

        //加入未查到的信息
        tree.addAll(categoryList);

        return JsonResult.ok().setData(tree);
    }

    @Override
    public JsonResult insertCategory(Category category) {
        if (category == null) {
            throw new ParameterException("参数对象为空");
        }

        if (StrUtil.isBlank(category.getName())) {
            throw new ParameterException("栏目名称不能为空");
        }

        if (StrUtil.isBlank(category.getType())) {
            throw new ParameterException("栏目类型不能为空");
        }

        if (category.getSort() == null) {
            category.setSort(9999);
        }

        baseMapper.insert(category);
        return JsonResult.ok("添加成功");
    }

    @Override
    public JsonResult updateCategory(Category category) {
        if (category == null) {
            throw new ParameterException("参数对象为空");
        }

        if (StrUtil.isBlank(category.getName())) {
            throw new ParameterException("栏目名称不能为空");
        }

        if (StrUtil.isBlank(category.getType())) {
            throw new ParameterException("栏目类型不能为空");
        }

        if (category.getSort() == null) {
            category.setSort(9999);
        }

        baseMapper.updateById(category);
        return JsonResult.ok("修改成功");
    }

    @Override
    public JsonResult getId(Integer id) {
        if (id == null) {
            throw new ParameterException("参数为空");
        }

        QueryWrapper<Category> wrapper = new QueryWrapper<>();
        wrapper.eq("id", id);
        Integer count = baseMapper.selectCount(wrapper);

        if (count != 1) {
            throw new ParameterException("栏目不存在");
        }

        Category category = baseMapper.selectById(id);
        return JsonResult.ok().setData(category);
    }

    @Override
    public JsonResult deleteById(Integer id) {
        if (id == null) {
            throw new ParameterException("参数对象为空");
        }

        QueryWrapper<Category> wrapper = new QueryWrapper<>();
        wrapper.eq("parent_id", id);
        List<Category> categoryList = baseMapper.selectList(wrapper);

        if (categoryList.size() > 0) {
            return JsonResult.error("请先删除子类栏目");
        }

        if (removeById(id)) {
            return JsonResult.ok("删除成功");
        }

        return JsonResult.error("删除失败");
    }

    @Override
    public List<Category> childrenList() {
        QueryWrapper<Category> wrapper = new QueryWrapper<>();
        wrapper.eq("type", 1);
       return baseMapper.selectList(wrapper);
    }

    @Override
    public List<Category> oldCategoryList() {
        QueryWrapper<Category> categoryQueryWrapper = new QueryWrapper<>();
        categoryQueryWrapper.isNotNull("old_id");
//        return baseMapper.selectList(categoryQueryWrapper);
        return baseMapper.selectOld();
    }


}
